﻿using System.Collections.Generic;
using Crowler.Dispatcher.BusinessComponents;
using Crowler.Model.Dispetcher.BusinessComponents;
using Crowler.Model.Dispetcher.DataAccess;
using Crowler.Model.Dispetcher.Domain;
using Crowler.TestBase;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;

namespace Crowler.Dispatcher.BusinessService.Test
{
    [TestClass]
    public class FetchUndescoveredEntryServiceShould:TestBaseClass
    {
        private GetNewEntryToDiscoverService _service;
        private Mock<IDiscoverableEntryBc> _discoverableEntryBcMock;
        private Mock<IDiscoverableEntryRepository> _discoverableEntryRepositoryMock;
        private List<IWebDomain> _domains;
        private const string TestUrl = "TestUrl";
        private const int UndiscoveredEntriesAmount = 2;
        private const int MyDomainsAmount = 3;

        public override void TestCleanup()
        {

        }

        public override void TestInitialize()
        {

            _discoverableEntryBcMock = new Mock<IDiscoverableEntryBc>();

            _discoverableEntryRepositoryMock  = new Mock<IDiscoverableEntryRepository>();
            _domains = new List<IWebDomain>();
           
            FillDomains();
            SetUpMocks();
            _service = new GetNewEntryToDiscoverService(_discoverableEntryBcMock.Object,_discoverableEntryRepositoryMock.Object);
        }

        private void FillDomains()
        {
            for (var i = 0; i < MyDomainsAmount; i++)
            {
                var tmpWebDomain = new WebDomain {DomainUrl = "www.kaka.com", FullyDiscovered = false};
                var tmpUndiscovered = new List<IDiscoverableEntry>();
                for (var j = 0; j < UndiscoveredEntriesAmount; j++)
                {
                    var tmpEntry = new DiscoverableEntry {Status = EntryStatus.New};
                    tmpUndiscovered.Add(tmpEntry);
                }
                tmpWebDomain.UndiscoveredEntries = tmpUndiscovered;
                _domains.Add(tmpWebDomain);
            }
        }

        private void SetUpMocks()
        {
            _discoverableEntryRepositoryMock.Setup(o => o.GetWorkingSetDomains()).Returns(_domains);
            _discoverableEntryBcMock.Setup(o => o.GetEntryToDiscover(_domains)).Returns(new DiscoverableEntry { Status = EntryStatus.New,EntryUrl = TestUrl});
        }


        [TestMethod]
        public void RetrieveNotFullyDiscoveredDomains()
        {
            _service.Execute();
            _discoverableEntryRepositoryMock.Verify(o => o.GetWorkingSetDomains());
        }

        [TestMethod]
        public void FindDomainToDiscoverBasedOnWorkersThreshold()
        {
            _service.Execute();
            _discoverableEntryBcMock.Verify(o => o.GetEntryToDiscover(It.IsAny<IEnumerable<IWebDomain>>()),Times.Once());
        }

        [TestMethod]
        public void ReturnValidResponseIfThereAreEntriesToDiscover()
        {
            _service.Execute();
            var result = _service.ServiceResponse;
            Assert.AreEqual(TestUrl,result.Url);
        }


        //[TestMethod]
        //public void ProcessRequest()
        //{
        //    var resourceText = "asdf asdf adsdf http://www.asdfasdfas.co.il/#q=%D7%asdfasdfasdf0%D7%A8%D7%93+%D7%9E%D7%99%D7%99%D7%91%D7%A8%D7%99%D7%93%D7%92%27&ct=muybridge12-hp&oi=ddle&bav=on.2,or.r_gc.r_pw.,cf.osb&fp=6e52109d99738d19&biw=1350&bih=980 asdfasd fasdf asdfasdf";
        //    Regex regx = new Regex("http(s)?://([\\w+?\\.\\w+])+([a-zA-Z0-9\\~\\!\\@\\#\\$\\%\\^\\&amp;\\*\\(\\)_\\-\\=\\+\\\\\\/\\?\\.\\:\\;\\'\\,]*)?", RegexOptions.IgnoreCase);

        //    MatchCollection matches = regx.Matches(resourceText);

        //    foreach (Match match in matches)
        //    {
        //        var url = match.Value;
        //        var uri = new Uri(url);
        //        var result = uri.Host;
        //    }   



        //    //ujhgjhg
        //    //var test = new Uri(.);
        //    //var result = test.Host;
        //    _toDiscoverService.Execute();
        //}
    }
}
